package com.suse.lhw.hms.foodserver.controller;

import com.suse.lhw.hms.foodserver.constant.ConstModelSelectStatus;
import com.suse.lhw.hms.foodserver.constant.FoodModelStatusConst;
import com.suse.lhw.hms.foodserver.domin.*;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.*;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.FIAndName;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.PackageAndAllModels;
import com.suse.lhw.hms.foodserver.service.*;
import com.suse.lhw.hms.foodserver.tools.FileUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.com.suse.lhw.security.controller
 * @ClassName: FoodUserController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/24 9:53
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/24 9:53
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("fooduser")
public class FoodUserController {
    @Autowired
    FFoodmodelService fFoodmodelService;
    @Autowired
    FUnitService fFoodmodelUnitService;
    @Autowired
    FFoodmodelToFFoodmodelUnitService fFoodmodelToFFoodmodelUnitService;
    @Autowired
    FFoodIfoService fFoodIfoService;
    @Autowired
    FPackageService packageService;
    @Autowired
    FPackageToFFoodmodelService packageToFFoodmodelService;
    @Autowired
    FFoodmodelInstanceService instanceService;
    @Autowired
    FCollectionService fCollectionService;


    @RequestMapping(value = "addAUnuploadedFoodModel")
    public String addAUnuploadedFoodModel(@RequestBody model_ifo_units m) {
        try {
            System.out.println("m是那啥：" + m.toString());
            addAFoodModel pojo = new addAFoodModel(m.getName(), m.getImgurl(), m.getFmdescription(), m.getIfodescroption(), m.getCarbohydrate(), m.getProtein(), m.getFat(), m.getTag(), m.getOrtherNutrient());
            List<FUnit> list = m.getFunit();
            System.out.println("传过来的pojo是+" + pojo + "传过来的list是" + list.toString());
            String result = fFoodmodelService.addAUnuploadedFoodModel(pojo, list);
            result += ",上传图片的名称是" + result;
            return result;
        } catch (RuntimeException e) {
            e.printStackTrace();
//            throw new RuntimeException();
            return "网络繁忙";
        }
    }

    @RequestMapping("deleteAFoodModel")
    public String deleteAFoodModel(@RequestParam int foodid) {
        //先删除实例
        List<FFoodmodelInstance> fFoodmodelInstances = instanceService.selectByFoodModelId(foodid);
        for (FFoodmodelInstance fFoodmodelInstance : fFoodmodelInstances) {
            instanceService.deleteByPrimaryKey(fFoodmodelInstance.getId());
        }
        String result = fFoodmodelService.deleteADefaultFoodModel(foodid);
        System.out.println(result);
        return result;
    }

    @RequestMapping("findAllFoodModelsOfDefault")
    public List<FFoodmodel> findAllFoodModelsOfDefault(Model model) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:查找所有的默认食品模型
         * @Date:10:08 2020/4/29
         * @Param:[model]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.domin.FFoodmodel>
         **/
        try {
            List<FFoodmodel> foodmodelList = fFoodmodelService.selectFFoodmodelsByStatus(FoodModelStatusConst.defaut);
            model.addAttribute("foodmodeList", foodmodelList);
            return foodmodelList;
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("eatAFoodByModelId")
    public String eatAFoodByModelId(int foodId, int unitId, int quanity, int eattime) {
        /**
         * @Author:Hongwei Li
         * @Description:吃掉食物,生成食品用例，通过单位和数量换算出食品消耗热量的多少
         * @Date:12:52 2020/4/29
         * @Param:[foodId, unitId, quanity, eattime]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        if (instanceService.eatAFoodByModelId(foodId, unitId, quanity, eattime, userId) == 0) {
            return "添加食品实例失败，要饿肚子哦！";
        }
        return "添加成功！";
    }

    @RequestMapping("splitOutAFoodByModelId")
    public String splitOutAFoodByInstanceId(int InstanceId) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:用户删除所吃过的食物(食物实例)
         * @Date:13:12 2020/4/29
         * @Param:[InstanceId]
         * @Return:java.lang.String
         **/
        if (instanceService.splitOutAFoodByModelId(InstanceId) == 0) {
            return "催吐失败！";
        }
        return "你成功把食物吐出来了，我就当你没吃过！";
    }

    @RequestMapping("addAUnUploadPackage")
    public String addAPackageOfUser(package_fmids p) {
        /**未测试cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
         * @Author:Hongwei Li
         * @Description:用户增加一个自定义的未上传套餐，并将生成套餐和食品模型的映射
         * @Date:20:58 2020/5/2
         * @Param:[fPackage, fmIds]
         * @Return:java.lang.String
         **/
        FPackage fPackage = new FPackage(p.getId(), p.getPackageCategory(), p.getName(), p.getDescription(), p.getStatus(), p.getUserId(), p.getImgUrl(), p.getSpare1(), p.getSpare2());
        List<Integer> fmIds = p.getFmIds();
        if (packageService.insertAnUnUploadPackage(fPackage, fmIds) == 0) {
            return "添加失败";
        } else {
            return "已经吃掉！！！";
        }
    }

    @RequestMapping("deleteAUnUploadPackage")
    public String deldeteAUnUploadPackage(int pakcageId) {
        /**
         * @Author:Hongwei Li
         * @Description:用户删除一个自定义的未上传套餐，前端传入一个套餐id，删除套餐和相关的映射
         * @Date:21:13 2020/5/2
         * @Param:[pakcageId]
         * @Return:java.lang.String
         **/
        if (packageService.deleteAnUnUplodPackage(pakcageId) == 0) {
            return "删除成功！";
        }
        return "删除失败";
    }

    @RequestMapping("uploadAPackage")
    public String uploadAPakcage(int packageId) {
        /**
         * @Author:Hongwei Li
         * @Description:上传一个用户自定义的套餐，直接将状态改为已上传
         * @Date:21:20 2020/5/2
         * @Param:[packageId]
         * @Return:java.lang.String
         **/
        FPackage fpackage = new FPackage();
        fpackage.setId(packageId);
        fpackage.setStatus(FoodModelStatusConst.UPLOAD);
        if (packageService.updateByPrimaryKeySelective(fpackage) == 0) {
            return "上传成功！";
        }
        return "上传失败！";
    }

    @RequestMapping("eatAPackage")
    public String eatAPackage(int status, int packaeid, List<eatAPackage> ptmList) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:用户吃掉一个套餐，通过前端传来的套餐id和套餐中的数量(eatAPackage中有modelId,数量和单位id)，来插入套餐实例和套餐中食品模型的实例
         * @Date:21:51 2020/5/2
         * @Param:[packaeid, ptmList]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        if (packageService.insertPTMs(packaeid, ptmList, userId, status) == 0) {
            return "没吃掉？还没吃呢";
        }
        return "添加成功！";
    }

    @PostMapping("splitOutAPackageByPackageId")
    public String splitOutAPackageByPackageId(int packageId) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:用户删除制定的套餐实例子，顺便删除相关的映射
         * @Date:0:38 2020/5/3
         * @Param:[packageId]
         * @Return:java.lang.String
         **/
        if (packageService.deleteAnUnUplodPackage(packageId) == 0) {
            return "删除失败";
        }
        return "删除成功！";
    }

    @RequestMapping("selectAllPackageByType")
    public List<PackageAndAllModels> selectAllDefaultPackage(int type) {
        return packageService.selectAllDefault(type);
    }

    @RequestMapping("collectAModelOrPackage")
    public String collectAModelOrPackage(int id, int type) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:用户收藏一个食品或者时套餐，按照传来的type参数添加进入收藏即可
         * @Date:0:39 2020/5/3
         * @Param:[id, type]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        FCollection fCollection = new FCollection();
        fCollection.setType(type);
        fCollection.setCreattime(new Date());
        fCollection.setCollectionid(id);
        fCollection.setUserid(userId);
        if (fCollectionService.insertSelective(fCollection) == 0)
            return "收藏失败";
        return "收藏成功";
    }

    @RequestMapping("cancelACollection")
    public String cancleACollectionById(int id) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:用户取消收藏
         * @Date:0:40 2020/5/3
         * @Param:[id]
         * @Return:java.lang.String
         **/
        if (fCollectionService.deleteByPrimaryKey(id) == 0) {
            return "取消收藏失败";
        }
        return "取消收藏成功！";
    }

    @RequestMapping("selectPackageCollection")
    public List<FPackage> selectPackageCollection() {
        /**
         * @Author:Hongwei Li
         * @Description:用户查找已经收藏的套餐
         * @Date:0:49 2020/5/3
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.domin.FFoodmodel>
         **/
        return fCollectionService.selectAllByType();
    }

    @RequestMapping("selectModelCollection")
    public List<FFoodmodel> selectCodelCollection() {
        /**
         * @Author:Hongwei Li
         * @Description:用户查找已经收藏的模型
         * @Date:1:04 2020/5/3
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.domin.FFoodmodel>
         **/
        return fCollectionService.selectALLModelCollection();
    }

    @RequestMapping("selectModelByNameStatusUserid")
    public List<modelAndUnits> selectModelByNameStatusUserid(@RequestParam(required = false) String name, @RequestParam(required = false) int status, @RequestParam(required = false) String userrId) {
        /**
         * @Author:Hongwei Li
         * @Description:根据传来的名称，状态和用户id查找所有对应的食品模型以及单位，其中，status直接又前端传入并按照ConstmodelSelectStatus类的规范
         * @Date:1:41 2020/5/31
         * @Param: [name, status, userId]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.pojo.input_POJO.modelAndUnits>
         **/
        String userId = "aaa";
        List<modelAndUnits> list = fFoodmodelService.selectByNameStatusUserid(name, status, userId);
        for (modelAndUnits modelAndUnits : list) {
            list.toString();
        }
        System.out.println();
        return list;
    }

    @RequestMapping("findAFoodModelById")
    public ModelUnitAndIfo findAFoodModelById(int id) {
        System.out.println("id si" + id);
        ModelUnitAndIfo modelAndUnits = fFoodmodelService.selectModelAndUnitsById(id);
        return modelAndUnits;
    }

    @RequestMapping("selectFIsByUserId")
    public List<FIAndName> selectFIsByUserId() {
        String userId = "aaa";
        List<FIAndName> list = instanceService.selectByUserId(userId);
        if (list == null)
            return null;
        else
            for (FIAndName instance : list) {
                System.out.println(instance.toString());
            }
        return list;
    }

}
